package com.tj.jinzao.activity;

import android.content.Intent;

import com.tj.jinzao.R;
import com.tj.jinzao.api.ApiRetrofit;
import com.tj.jinzao.api.ApiService;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.widgets.ChooseConfigDialog;

import java.util.HashMap;

import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 发布蒙版界面
 * Created by wjy on 2019/1/5.
 */
public class PublishMaskActivity extends BaseActivity {
    protected ApiService mApiService = ApiRetrofit.getInstance().getApiService();

    private CompositeSubscription mCompositeSubscription;


    public void detachView() {
        onUnsubscribe();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        detachView();
    }
    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    //RXjava取消注册，以避免内存泄露
    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }


    @OnClick(R.id.iv_cancel)
    public void exit(){
        finish();
        overridePendingTransition(R.anim.push_bottom_out,0);
    }

    @OnClick(R.id.ll_publish_daily_layout)
    public void publishDaily(){
        startActivity(new Intent(this, PublicDailyActivity.class));
        finish();
    }
    private ChooseConfigDialog mDialog;
    private void dialog(String content){
        mDialog = new ChooseConfigDialog(PublishMaskActivity.this, content);
        mDialog.show();
        mDialog.setOnClickLinstener(new ChooseConfigDialog.OnClickLinstener() {
            @Override
            public void onConfig(String mContent) {
                mDialog.dismiss();
                finish();
            }
        });
    }

    @OnClick(R.id.ll_publish_topic_layout)
    public void publishTopic(){
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("uid", UserManager.getInstance().getUser().getId());
        addSubscription(mApiService.getJCS(hashMap), new SubscriberCallBack<String>(this) {
            @Override
            protected void onSuccess(String list) {
                if(list.equals("true")){
                    startActivity(new Intent(PublishMaskActivity.this, PublicTopic1Activity.class));

                }else {
                    if(list!=null&&!list.equals("")){
                        dialog(list);
                    }else {
                        dialog("身份认证通过并达到V2等级才可以发布话题哦！");
                    }

                }

            }

            @Override
            protected void onFailure(ResultResponse response) {
                dialog("身份认证通过并达到V2等级才可以发布话题哦！");


            }

            @Override
            protected boolean isShowProgress() {
                return false;
            }

            @Override
            protected void onPageSuccess(int currentPage, int allPage, int allSize) {
            }
        });


    }


    @Override
    public int getLayoutResource() {
        return R.layout.activity_masking;
    }

}
